<?php

namespace App\Admin\Controllers\Manger;

use App\Admin\Validate\Manger\Company;
use Illuminate\Http\Request;

use App\Http\Controllers\Controller;
use App\Models\Admin\AdminUser;
use App\Models\Company as ModelsCompany;
use App\Models\Manger\Manger;
use App\Services\Manger\MangerAccount;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class CompanyController extends Controller
{
    /**
     * 新增编辑
     */
    public function dopost(Request $request)
    {
        $req_data = $request->only(['id', 'company_name', 'other_title','cpd_id','org_id']);

        $Validate = new Company;
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        if (isset($req_data['id']) && $req_data['id'] > 0) {
            $isexist = ModelsCompany::where([['id', '!=', $req_data['id']], ['company_name', $req_data['company_name']],['status',1]])->orWhere([['id', '!=', $req_data['id']], ['other_title', $req_data['other_title']],['status',1]])->first();
        } else {
            $isexist = ModelsCompany::where([['company_name', $req_data['company_name']],['status',1]])->orWhere([['other_title', $req_data['other_title']],['status',1]])->first();
        }

        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '公司名或账套名已存在']);
        }


        //判断账套信息是否包含超管信息

        $isexist = AdminUser::where([['username', $req_data['company_name']]])->orWhere([['username', $req_data['other_title']]])->first();

        if ($isexist) {
            return response()->json(['code' => '001', 'msg' => '账套信息不可与超管信息相同']);
        }

        $isExistOrgId = ModelsCompany::where("org_id",$req_data["org_id"])->first();
        if($isExistOrgId){
            return response()->json(['code' => '001', 'msg' => '企业已绑定']);
        }

        $data = [
            'company_name' => $req_data['company_name'],
            'other_title' => $req_data['other_title'],
            // 'cpd_id'=>$req_data['cpd_id'],
            'update_at' => time()
        ];
        if (isset($req_data['id']) && $req_data['id'] > 0) {
            ModelsCompany::where([['id', $req_data['id']]])->update($data);
            return response()->json(['code' => '000', 'msg' => '编辑成功']);
        } else {
            $data["org_id"] = $req_data["org_id"];
            $data['create_at'] = time();
            $cpid=ModelsCompany::create($data)->id;

            DB::table('sale_rule')->insert(['cpid'=>$cpid,'create_at'=>time(),'update_at'=>time()]);
            return response()->json(['code' => '000', 'msg' => '新增成功']);
        }

    }
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $limit = $request->limit ?? 10;
        $isgetall = $request->isgetall ?? 0;
        $status = $request->status ?? 0;
        $query = ModelsCompany::select(['companyinfo.id', 'companyinfo.company_name', 'companyinfo.other_title', 'companyinfo.status','companyinfo.org_id','tplus_account.accName'])
                ->leftJoin('tplus_account', 'companyinfo.org_id', '=', 'tplus_account.org_id')
                ->orderBy('companyinfo.create_at', 'desc');
        $company_name = $request->company_name;
        if ($company_name != '') {
            $query->where('companyinfo.company_name', 'like', '%' . $company_name . '%');
        }
        if (in_array($status, [1, 2])) {
            $query->where('companyinfo.status', $status);
        }

        if ($isgetall == 0) {
            $list = $query->paginate($limit)->toArray();
            return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => paginateDataToArray($list)]);
        } else {
            $list = $query->get()->toArray();
            return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['list' => $list]]);
        }
    }

    /**
     * 冻结
     */
    public function changestatus(Request $request)
    {
        $id = $request->id ?? 0;
        $info = ModelsCompany::where([['id', $id]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $status = 2;
        ModelsCompany::where([['id', $id]])->update(['status' => $status, 'update_at' => time()]);
        //冻结需删除管理员以及子账号登录状态
        $mangerlist=Manger::where([['cpid', $id]])->get()->toArray();
        foreach($mangerlist as $val){
            Manger::where([['cpid', $id],['id',$val['id']]])->update(['status'=>2,'update_at'=>time()]);
            MangerAccount::cleartokenkey($val['id']);
        }



        return response()->json(['code' => '000', 'msg' => '操作成功']);


    }

}
